package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.DeviceBean;
import com.emip.bean.QualityTypeBean;
import com.emip.dao.BaseDAO;
import com.emip.dao.DeviceDAO;

@Repository("deviceDAO")
public class DeviceDAOImpl extends BaseDAO implements DeviceDAO {

	public int add(DeviceBean entity) {
		Integer id = (Integer)this.getHibernateTemplate().save(entity);
		return id.intValue();
	}

	public DeviceBean findByID(Integer ID) {
		DeviceBean bean = this.getHibernateTemplate().get(DeviceBean.class, ID);
		return bean;
	}

	public List<DeviceBean> getAll() {
		List<DeviceBean> result = this.getHibernateTemplate().find("from DeviceBean m order by ID");
		return result;
	}

	public void remove(DeviceBean entity) {
		this.getHibernateTemplate().delete(entity);
	}

	public void update(DeviceBean entity) {
		this.getHibernateTemplate().update(entity);
	}

	public Boolean findExist(DeviceBean entity) {
		Object[] params = new Object[] {entity.getMonitorType(), entity.getDevicename()};
		List<DeviceBean> result = this.getHibernateTemplate().find("from DeviceBean m where monitorType=? and devicename=?", params);
		if (result.size()==0) {
			return false;
		}
		else {
			//是否存在的那条记录的monitorType 和 devicename 和自己是相同的，如果是的话，那么其实是update其他字段的一种操作，也是合法操作
			if (entity.getId()==0) {  //新增的情况
				return true;
			} else {
				DeviceBean eBean = this.getHibernateTemplate().get(DeviceBean.class, entity.getId());
				if (entity.getMonitorType().getId() == eBean.getMonitorType().getId() &&
						entity.getDevicename().equals(eBean.getDevicename())) {
					return false;
				} else {
					return true;
				}
			}
		}
	}

}
